package com.cx.algrithm.sort;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.JsonProcessingException;
import jdk.internal.org.objectweb.asm.ClassWriter;
import jdk.internal.org.objectweb.asm.Opcodes;

import java.util.ArrayList;
import java.util.List;

public class ComparablePrinciple {
    public static void main(String[] args) throws JsonProcessingException, InterruptedException {
        Sub sub = new Sub();
//        System.out.println(sub.test());
//        sub.test2();

//        Dept d = new Dept();
//        d.setName("Market");
//        Emp e1 = new Emp();
//        e1.setName("zhangsan");
//        e1.setDept(d);
//        Emp e2 = new Emp();
//        e2.setName("lisi");
//        e2.setDept(d);
//        d.setEmp(Arrays.asList(e1,e2));
//        ObjectMapper mapper = new ObjectMapper();
//        System.out.println(mapper.writeValueAsString(d));

//        Demo1_5.test();

//            Demo1_4.test();
        Demo1_8.test();
    }
}

class Father {
    int i = 2;

    Father() {
        System.out.println(i);
        test2();
    }

    public short test() {
        System.out.println("fa test");
        return 32767;
    }

    public static short test2() {
        System.out.println("fa static test");
        return 32767;
    }
}

class Sub extends Father {
    int i = 3;
    int s = 3;

    public short test() {
        System.out.println(super.i);
        System.out.println("sub test");
        super.test();
        return 0;
    }

    public static short test2() {
        System.out.println("sub static test");
        return 32767;
    }
}

abstract class AbstractTest {

}

class Emp {
    private String name;
    @JsonIgnore
    private Dept dept;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setDept(Dept dept) {
        this.dept = dept;
    }

    public Dept getDept() {
        return dept;
    }
}

class Dept {
    private String name;
    private List<Emp> emp;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<Emp> getEmp() {
        return emp;
    }

    public void setEmp(List<Emp> emp) {
        this.emp = emp;
    }
}

//内存溢出
class Demo1_5 {
    public static void test() {
        int i = 0;
        try {
            List<String> list = new ArrayList<>();
            String a = "hello";
            while (true) {
                list.add(a);
                a = a + 1;
                i++;
            }
        } catch (Throwable e) {
            e.printStackTrace();
            System.out.println(i);
        }
    }
}

class Demo1_4 {
    public static void test() throws InterruptedException {
        System.out.println("1....");
        Thread.sleep(30000);
        byte[] array = new byte[1024 * 1024 * 10];
        System.out.println("2....");
        Thread.sleep(30000);
        array = null;
        System.gc();
        System.out.println("3....");
        Thread.sleep(1000000L);
    }
}

// -XX:MaxMetaspaceSize=8m

class Demo1_8 extends ClassLoader {
    public static void test() {
        int j = 0;
        try {
            Demo1_8 test = new Demo1_8();
            for (int i = 0; i < 100000; i++, j++) {
                System.out.println(i);
                ClassWriter cw = new ClassWriter(0);
                cw.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, "Class" + i, null, "java/lang/Object", null);
                byte[] code = cw.toByteArray();
                test.defineClass("Class" + i, code, 0, code.length);
            }
        } finally {
            System.out.println(j);
        }
    }
}

class Demo1_9 {

}

